package BookSystem.operation;

import BookSystem.book.BookList;

import java.util.Scanner;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 */
public class DelOperation implements IOperation{

    @Override
    public void work(BookList bookList) {
        int currentSize = bookList.getUseSize();
        if (currentSize == 0){
            System.out.println("已经没有书了！");
            return;
        }
        System.out.println("请输入要删除的书名: ");
        Scanner in = new Scanner(System.in);
        String name = in.nextLine();
        int index = -1;
        for (int i = 0; i < currentSize; i++) {
            if (name.equals(bookList.getBook(i).getName())){
                index = i;
                break;
            }
        }
        if (index == -1){
            System.out.println("要删除的书不存在！");
            return;
        } else {
            // 删除操作
            for (int i = index; i < currentSize - 1; i++) {
                bookList.setBook(i, bookList.getBook(i + 1));
            }
            bookList.setBook(currentSize - 1, null);
            bookList.setUseSize(currentSize - 1);
            System.out.println("删除成功！");
        }
    }
}
